import React from "react";
import { Breadcrumb } from "antd";
import { Link } from "react-router-dom";

export const menuData2BreadcrumbData = (
  menuData,
  path = window.location.pathname
) => {
  for (let i = 0; i < menuData.length; i += 1) {
    const menuDatum = menuData[i];
    const { children, link, hiddenMenu } = menuDatum;
    if (link === path && !hiddenMenu) {
      return [menuDatum];
    }
    if (children instanceof Array) {
      const subArr = menuData2BreadcrumbData(children, path);
      if (subArr.length) {
        return [menuDatum].concat(subArr);
      }
    }
  }
  return [];
};

export default ({ data = [], onMenuItemClick, ...otherProps }) => {
  const newBreadcrum = [];
  const LEN = data.length;
  for (let i = 0; i < LEN; i += 1) {
    const { key, title, link } = data[i];
    newBreadcrum.push(
      <Breadcrumb.Item key={key}>
        {link && (i === LEN - 2) ? <Link to={link} onClick={(e) => { onMenuItemClick(e, link) }}>{title}</Link> : <span>{title}</span>}
        {/* {link && (i === LEN - 1) ? <Link to={window.location.pathname+window.location.search}>{title}</Link> : null} */}
        {/* {i + 1 === LEN ? <Icon type="sync" style={{ marginLeft: 16 }} onClick={() => window.location.reload()} /> : null} */}
      </Breadcrumb.Item>
    );
  }
  return <Breadcrumb {...otherProps}>{newBreadcrum}</Breadcrumb>;
};
